% 线性平流方程的 Lax-Wendroff Scheme 求解。使用了周期性边界
% 线性平流方程是 du/dt +v du/dx=0
% Gitee Repo

clc
clear

L = 2;
dx = 0.05;
dt = 0.001;
v = 1;
TICK=5000;

sigma = v*dt/dx;
if sigma > 1
    warning('may not converge.')
end

x = (-L:dx:L)';
n = size(x,1);

u0 = zeros(n,1);
u1 = zeros(n,1);

u0 = exp(-2*x.^2);

figure();
k=0;

for tick = 1:TICK
    u1 = 1/2*sigma*(1+sigma)*circshift(u0,1,1)+ (1-sigma^2)*u0 -1/2*sigma*(1-sigma)*circshift(u0,-1,1);
    u0 = u1;

    if mod(tick,100)==0
        printf("t=%f\n",tick*dt)
        clf
        hold on
        axis equal
        axis([-L L -2 2])
        plot(x,u1)
        pause(0.1)
    end
end

